﻿using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using MarqueeMoment.Entities;
using NLog;
using System.IO;

namespace MarqueeMoment.Forms
{
    public partial class frmReviewPhotos : Form
    {
        #region User defined Variables
        private static Logger logger = LogManager.GetCurrentClassLogger();
        #endregion User defined Variables

        #region Constructor

        public frmReviewPhotos()
        {
            logger.Info("On frmReviewPhotos()");
            InitializeComponent();
            InitializeValues();
        }
        #endregion Constructor

        #region User Defined Methods

        private void InitializeGridList()
        {
            logger.Info("On InitializeGridList()");
            dgImage.Rows.Clear();
            try
            {
                if (!Directory.Exists(Common.capturedPhotoDirPath))
                    Directory.CreateDirectory(Common.capturedPhotoDirPath);

                logger.Info("loading image files from " + Common.capturedPhotoDirPath);
                String[] strFilesName = System.IO.Directory.GetFiles(Common.capturedPhotoDirPath);
                //String[] strFilesName = System.IO.Directory.GetFiles(@"C:\Documents and Settings\Administrator\Desktop\ImageFolder\");
                if (strFilesName.Count() > 0)
                {
                    int rowIndex = 0;
                    for (Int32 count = 0; count < strFilesName.Count(); count++)
                    {
                        String fileName = "Photo" + Common.imageIndex;
                        if (strFilesName[count].Contains(fileName))
                        {
                            Bitmap tempImage = new Bitmap(strFilesName[count].ToString());
                            Bitmap img = new Bitmap(tempImage);
                            tempImage.Dispose();

                            dgImage.Rows.Add();
                            dgImage.Rows[rowIndex].Cells["IdCol"].Value = string.Empty;
                            dgImage.Rows[rowIndex].Cells["ImageData"].Value = img;
                            rowIndex++;
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                logger.Error(exc.StackTrace);
                Dialogs.Error("Error on showing Capture Photos list");

            } 
        }

        /// <summary>
        /// For releasing image from picture box
        /// </summary>
        private void ReleaseSource()
        {
            logger.Info("On ReleaseSource()");
            if (picboxPhoto.Image != null)
                picboxPhoto.Image.Dispose();

            picboxPhoto.Image = null;
            if (dgImage != null)
            {
                dgImage.Rows.Clear();
                dgImage.Dispose();
            }
        }

        /// <summary>
        /// To Initialize Values
        /// </summary>
        private void InitializeValues()
        {
            logger.Info("On InitializeValues() at frmReviewPhotos form");
            ShowTime();
            InitializeGridList();
        }

        /// <summary>
        /// To Show Time on Time Box
        /// </summary>
        private void ShowTime()
        {
            String currentTime = String.Empty;
            var timeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
            currentTime = TimeZoneInfo.ConvertTime(DateTime.Now, timeZone).ToLongTimeString();
            lblPM.Text = currentTime.Substring(currentTime.Length - 2);
            currentTime = currentTime.Substring(0, currentTime.Length - 3);

            lblT8.Text = currentTime.Substring(currentTime.Length - 1);
            lblT7.Text = currentTime.Substring(currentTime.Length - 2, 1);
            lblT6.Text = currentTime.Substring(currentTime.Length - 3, 1);
            lblT5.Text = currentTime.Substring(currentTime.Length - 4, 1);
            lblT4.Text = currentTime.Substring(currentTime.Length - 5, 1);
            lblT3.Text = currentTime.Substring(currentTime.Length - 6, 1);
            lblT2.Text = currentTime.Substring(currentTime.Length - 7, 1);

            if (currentTime.Length == 7)
                lblT1.Text = String.Empty;
            else
                lblT1.Text = currentTime.Substring(currentTime.Length - 8, 1);
        }

        #endregion User Defined Methods

        #region System Events

        private void btnComposeMessage_Click(object sender, EventArgs e)
        {
            logger.Info("On btnComposeMessage_Click(object sender, EventArgs e)");
            if (dgImage.RowCount > 0 && dgImage.CurrentCell.RowIndex >= 0)
            {
                String imagePath = Common.GetImagePath(dgImage.CurrentCell.RowIndex);
                if (File.Exists(imagePath))
                {
                    if (picboxPhoto.Image != null)
                        picboxPhoto.Image.Dispose();
                    picboxPhoto.Image = null;
                    
                    Common.SaveUpdatedSizePhoto(imagePath,false);
                    
                    this.Visible = false;
                    frmComposeMessage obComposeMessage = new frmComposeMessage(true);
                    obComposeMessage.ShowDialog();
                    this.Close();
                }
            }
        }

        private void btnCameraSwitch_Click(object sender, EventArgs e)
        {
            logger.Info("On btnCameraSwitch_Click(object sender, EventArgs e)");
            picboxPhoto.Focus();
            ReleaseSource();

            this.Visible = false;
            frmTakePhoto obTakePhoto = new frmTakePhoto(false, true);
            if (obTakePhoto.isCameraDetected)
            {
                obTakePhoto.ShowDialog();
                this.Close();
            }
        }

        private void timerReviewPhoto_Tick(object sender, EventArgs e)
        {
            ShowTime();    
        }

        private void btnMainmenu_Click(object sender, EventArgs e)
        {
            logger.Info("On btnMainmenu_Click at frmReviewPhotos form");            
            this.Close(); 
        }

        private void dgImage_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            logger.Info("On dgImage_CellEnter(object sender, DataGridViewCellEventArgs e)");
            if (dgImage!=null && dgImage.RowCount > 0 && e.RowIndex >= 0)
            {
                logger.Info("On ");
                String imagePath = Common.GetImagePath(e.RowIndex);
                if (File.Exists(imagePath))
                {
                    if (picboxPhoto.Image != null)
                        picboxPhoto.Image.Dispose();

                    Bitmap tempImage = new Bitmap(imagePath);
                    Bitmap img = new Bitmap(tempImage);
                    tempImage.Dispose();

                    picboxPhoto.Image = (Image)img;
                }
            }
        }

        private void frmReviewPhotos_FormClosing(object sender, FormClosingEventArgs e)
        {
            logger.Info("On frmReviewPhotos_FormClosing(object sender, FormClosingEventArgs e)");
            ReleaseSource();
        }

        #endregion System Events

    }
}
